/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.vng.zing.zprofilemiddleware.common;

import com.vng.zing.logger.ZLogger;
import java.io.IOException;
import java.security.MessageDigest;
import org.apache.log4j.Logger;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

/**
 *
 * @author namhh
 */
public class Encription {
    private static final Logger _Logger = ZLogger.getLogger(Encription.class);
    private static  final int LOOP_COUNT = 10;

    public static String getHash( String password, String salt) {
        try {
            MessageDigest digest = MessageDigest.getInstance("SHA-1");
            digest.reset();
            digest.update(base64ToByte(salt));
            byte[] input = digest.digest(password.getBytes("UTF-8"));
            for (int i = 0; i < LOOP_COUNT; i++) {
                digest.reset();
                input = digest.digest(input);
            }
            String a = byteToBase64(input);
            return a;
        } catch (Exception ex) {
            _Logger.error("encript fail " + password);
        }
        return null;
    }
    
      public static byte[] base64ToByte(String data) throws IOException {
       BASE64Decoder decoder = new BASE64Decoder();
       return decoder.decodeBuffer(data);
   }
 
   /**
    * From a byte[] returns a base 64 representation
    * @param data byte[]
    * @return String
    * @throws IOException
    */
   public static String byteToBase64(byte[] data){
       BASE64Encoder endecoder = new BASE64Encoder();
       return endecoder.encode(data);
   }
}
